#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: sdr1k_controller.py
Date: January 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

The hardware interface to the SDR1000

"""

#-----------------------------------------------------------------------
# System imports
import logging
import sys
import os

# Application imports
import sdr1khw
import utils

#======================================================================
# The SDR1000 controller class
# This is just a wrapper onto sdr1khw.py which is the SWIG generated file
# that binds to the 'C' shared library where the actual controller code lives.
class Sdr1kController:
    
    #-------------------------------------------------------------------
    def __init__(self, persist, pub, rfe, pa, usb, addr):
        
        # Stash the event service proxy
        self.pub= pub
        
        # Initialise the frequency
        self.freq = 0.0
        
        # Capabilities
        self.cap_low_freq = utils.persist_get(persist, 'Capability', 'lowFreq')
        self.cap_high_freq = utils.persist_get(persist, 'Capability', 'highFreq')
        
        # Initialise the controller.
        self.hw = None
        self.hw = sdr1khw.SDR1000('acorn', rfe, pa, usb, addr)
        
    #-------------------------------------------------------------------
    # Message handlers
    # Note some also reflect the change. Try not to double up on these messages.
    #-------------------------------------------------------------------
    def power_on(self):
        self.hw.PowerOn()
        
    #-------------------------------------------------------------------
    def power_off(self):
        self.hw.StandBy()
        
    #-------------------------------------------------------------------
    def freq_set(self, freq):
        self.hw.SetFreq(freq)
        self.freq = freq
        self.pub.freqUpdate(self.freq)
        
    #-------------------------------------------------------------------
    def freq_inc(self, inc):
        new_freq = self.freq + inc
        if new_freq >= self.cap_low_freq and new_freq <= self.cap_high_freq:
            self.freq = self.freq + inc
            self.hw.SetFreq(self.freq)
            self.pub.freqUpdate(self.freq)
    